<template>
  <view class="container">
    <uni-forms :modelValue="form" label-position="top" label-width="220">
      <uni-forms-item :label="`${$t('login.address')}`" name="form.address">
        <uni-easyinput v-model="form.address" />
      </uni-forms-item>
      <uni-forms-item :label="`${$t('login.account')}`" name="account">
        <uni-easyinput v-model="form.account" />
      </uni-forms-item>
      <uni-forms-item :label="`${$t('login.password')}`" name="password">
        <uni-easyinput type="password" v-model="form.password" />
      </uni-forms-item>
    </uni-forms>
    <view class="container">
      <button type="primary" @click="toLogin">
        {{ $t('login.loginbtn') }}
      </button>
    </view>
  </view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';

export default {
  data() {
    return {
      form: {
        address: '',
        account: '',
        password: ''
      },
      token: ''
    };
  },
  computed: {},

  onShow() {
    this.form.address = uni.getStorageSync('__address') || '';
    this.form.account = uni.getStorageSync('__account') || '';
    this.form.password = uni.getStorageSync('__password') || '';

    console.log(this.form.address, 'onshow-address');
    console.log(this.form.account, 'onshow-account');
    console.log(this.form.password, 'onshow-__password');
  },
  onLoad() {},
  methods: {
    checkURL(URL) {
      var str = URL;
      //判断URL地址的正则表达式为:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
      //下面的代码中应用了转义字符"\"输出一个字符"/"
      var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
      var objExp = new RegExp(Expression);
      if (objExp.test(str) == true) {
        return true;
      } else {
        return false;
      }
    },
    async toLogin() {
      // uni.redirectTo({url:'/pages/home/index'})
      if (this.form.address === '') {
        uni.showToast({
          title: this.$t('login.address.tip'),
          icon: 'none'
        });
        return;
      }
      if (!this.checkURL(this.form.address)) {
        uni.showToast({
          title: this.$t('login.address.formtip'),
          icon: 'none'
        });
        return;
      }
      if (this.form.account === '') {
        uni.showToast({
          title: this.$t('login.account.tip'),
          icon: 'none'
        });
        return;
      }
      if (this.form.password === '') {
        uni.showToast({
          title: this.$t('login.password.tip'),
          icon: 'none'
        });
        return;
      }
      uni.showLoading({
        title: this.$t('login.loging')
      });
      const langTypeList = {
        zh_CN: 'zh_CN',
        en_US: 'en_US',
        fr_FR: 'fr_FR'
      };
      let systemInfo = uni.getSystemInfoSync();
      let url = this.form.address;
      console.log(systemInfo, '-systemInfo');
	  console.log('url: ', url);
      uni.request({
        // url: `${this.form.address}/auth/login`,
        url: `${url}/auth/login`,
        method: 'POST', // 请求方法
        data: {
          username: this.form.account,
          password: this.form.password,
          LangType: langTypeList[systemInfo.language]
        },
        success: (res) => {
          uni.hideLoading();
          // console.log(res, '-----succcess,res')
          if (res.data && res.statusCode === 200 && res.data.code === 0) {
            uni.showToast({
              title: this.$t('login.login.success'),
              icon: 'none'
            });
            uni.setStorageSync('__address', this.form.address);
            uni.setStorageSync('__account', this.form.account);
            uni.setStorageSync('__password', this.form.password);
            // console.log(res.header,'----head')
            // #ifdef H5
            uni.setStorageSync('__token', res.header.authorization);
            uni.setStorageSync(
              '__refresh_token',
              res.header['refresh-authorization']
            );
            //#endif
            // #ifdef APP-PLUS
            uni.setStorageSync('__token', res.header.Authorization);
            uni.setStorageSync(
              '__refresh_token',
              res.header['Refresh-Authorization']
            );
            //#endif
            uni.redirectTo({ url: '/pages/home/index' });
          } else {
            uni.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          uni.hideLoading();
          console.log('GET 请求失败', err);
          uni.showToast({
            title: err.errMsg,
            icon: 'none'
          });
        }
      });
    }
  }
};
</script>

<style lang="scss">
.headimg-box {
  image {
    width: 130rpx;
    height: 130rpx;
  }
}

.login-area {
  view {
    width: 25vw;
  }
}

.bind-area {
  width: 100vw;
  padding: 80rpx;
  padding-top: 100rpx;

  .field {
    background-color: #f5f5f5;

    textarea {
      height: 100rpx !important;
    }
  }
}

.container {
  margin: 0 10rpx 0 10rpx;
}
</style>
